#include "msgproc.h"

#include "execsql.h"
#include <QStringBuilder>
#include <QDebug>
#include "globalvars.h"

#include "devices.h"

extern int ledHall;
extern int ledRoom;
extern int ledKitc;
extern int ledLave;
extern int beepWar;

MsgProc::MsgProc(QThread *parent) :
    QThread(parent)
{
    m_isExit = false;
}

void MsgProc::exitThread(void)
{
    m_isExit = true;
}

void MsgProc::run()
{
    while(!m_isExit)
    {
        if(!GlobalVars::g_msgQueue.isEmpty())
        {
            QString msg = GlobalVars::g_msgQueue.dequeue();
            parseUserAsk(msg);
        }
        msleep(20);
    }
}

///解析通用请求命令
void MsgProc::parseUserAsk(QString msg)
{
    QStringList list = msg.split("#");
    int cmd = msg.at(0).toLatin1();
    switch (cmd) {
    ///通用请求命令
    case CMD_ChangePswd_H: parseChangePswd(list.at(1)); break;
    case CMD_HomeChat_C: parseHomeChat(list.at(1)); break;
    ///学生请求命令
    case CMD_DevTemp_T: parseDevTemp(list.at(1)); break;
    case CMD_DevHumi_H: parseDevHumi(list.at(1)); break;
    case CMD_DevMode_M: parseDevMode(list.at(1)); break;
    case CMD_DevStat_S: parseDevStat(list.at(1)); break;
    case CMD_DevBeep_B: parseDevBeep(list.at(1)); break;
    case CMD_DevLeds_D: parseDevLeds(list.at(1)); break;
    case CMD_DevOLED_O: parseDevOled(list.at(1)); break;
    default:
        break;
    }
}

///通用请求命令 - 登录退出在MsgSocket中实现
void MsgProc::parseChangePswd(QString data)
{

}

void MsgProc::parseHomeChat(QString data)
{
    qDebug() << "MsgProc::parseHomeChat():";
    QStringList list = data.split("|");
    emit signalSendChatToUser(list.at(0), list.at(1), list.at(2));
}

///解析设备控制命令
void MsgProc::parseDevTemp(QString data)
{
    ///定时获取温度数据
    QString msg = QString(CMD_DevTemp_T)
            % QString("#") % GlobalVars::g_tempStr
            % QString("|") % GlobalVars::g_tempStrMax
            % QString("|") % GlobalVars::g_tempStrMin;
    emit signalSendMsgToClient(data, msg);
}

void MsgProc::parseDevHumi(QString data)
{
    ///定时获取温度数据
    QString msg = QString(CMD_DevHumi_H)
                    % QString("#") % GlobalVars::g_humiStr
                    % QString("|") % GlobalVars::g_humiStrMax
                    % QString("|") % GlobalVars::g_humiStrMin;
     emit signalSendMsgToClient(data, msg);
}

void MsgProc::parseDevMode(QString data)
{
    ///定时获取温度数据
    QString msg = QString(CMD_DevMode_M)
            % QString("#") % GlobalVars::g_workMode
            % QString("|") % GlobalVars::g_warnInfo;
    emit signalSendMsgToClient(data, msg);
}

void MsgProc::parseDevStat(QString data)
{
    ///定时获取
    QString msg = QString(CMD_DevStat_S)
            % QString("#") % QString::number(ledHall)
            % QString("|") % QString::number(ledRoom)
            % QString("|") % QString::number(ledKitc)
            % QString("|") % QString::number(ledLave)
            % QString("|") % QString::number(beepWar);
    emit signalSendMsgToClient(data, msg);
}

void MsgProc::parseDevBeep(QString data)
{
    beep_ctrl();
}
void MsgProc::parseDevLeds(QString data)
{
    if(data == "1")
    {
        ledHall = !ledHall;
        led_hall(ledHall);
        beep_tips();
    }else if(data == "2")
    {
        ledRoom = !ledRoom;
        led_room(ledRoom);
        beep_tips();
    }else if(data == "3")
    {
        ledKitc = !ledKitc;
        led_kitc(ledKitc);
        beep_tips();
    }else if(data == "4")
    {
        ledLave = !ledLave;
        led_lave(ledHall);
        beep_tips();
    }
}
void MsgProc::parseDevOled(QString data)
{

}

